import 'dart:io';
import 'services/simple_api_client.dart';
import 'models/models.dart';

void main() async {
  print('=== Flutter 加密通讯测试 ===');
  print('');
  
  try {
    // 1. 健康检查
    print('🔍 测试健康检查...');
    final healthResult = await SimpleApiClient.healthCheck();
    print('✅ 健康检查成功: $healthResult');
    print('');
    
    // 2. 登录测试
    print('🔐 测试登录...');
    final loginResult = await SimpleApiClient.login('admin', 'password123');
    print('✅ 登录成功:');
    print('   Token: ${loginResult.token}');
    print('   User ID: ${loginResult.userId}');
    print('   Name: ${loginResult.name}');
    print('');
    
    // 3. 获取用户信息
    print('👤 测试获取用户信息...');
    final userResult = await SimpleApiClient.getUser();
    print('✅ 获取用户成功:');
    print('   ID: ${userResult.id}');
    print('   Name: ${userResult.name}');
    print('   Email: ${userResult.email}');
    print('   Age: ${userResult.age}');
    print('   Phone: ${userResult.phone}');
    print('');
    
    // 4. 获取用户列表
    print('👥 测试获取用户列表...');
    final usersResult = await SimpleApiClient.getUsers();
    print('✅ 获取用户列表成功 (${usersResult.length} 个用户):');
    for (final user in usersResult) {
      print('   - ${user.name} (${user.email})');
    }
    print('');
    
    // 5. 创建用户
    print('➕ 测试创建用户...');
    final newUser = UserData(
      id: 0,
      name: 'Test User',
      email: 'test@example.com',
      age: 25,
      phone: '+1234567890',
    );
    final createResult = await SimpleApiClient.createUser(newUser);
    print('✅ 创建用户成功:');
    print('   ID: ${createResult.id}');
    print('   Name: ${createResult.name}');
    print('   Email: ${createResult.email}');
    print('');
    
    print('🎉 所有测试完成！');
    
  } catch (e) {
    print('❌ 测试失败: $e');
    print('');
    print('📝 请确保:');
    print('   1. Golang服务器正在运行 (cd golang_server && go run main.go)');
    print('   2. 服务器地址正确 (http://localhost:8080)');
    print('   3. 网络连接正常');
    exit(1);
  }
}
